package com.sqsf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqsf.javabean.Role;
import com.sqsf.javabean.Users;
import com.sqsf.service.RoleService;
import com.sqsf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/users")
@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    //跳转登录页面
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    //用户登录
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpSession session,String username, String password){
        Users users = userService.login(username, password);
        if(users==null){
            session.setAttribute("msg","登录失败！");
            return "login";
        }else{
            session.setAttribute("users",users);
            return "main";
        }
    }

    //安全退出
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    //用户查询
    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit){
        PageHelper.startPage(page, limit);
        List<Users> all = userService.findAll();
        PageInfo<Users> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/users/users-list";
    }

    //跳转添加页面
    //在进行添加时 需要选择添加用户对应的角色 所以跳转到添加页面时
    //需要携带所有的角色信息
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(HttpServletRequest request){
        List<Role> all = roleService.findAll();
        request.setAttribute("roleList",all);
        return "users/users-add";
    }
    //添加用户
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Users users,int roleId ){
        userService.add(users,roleId);
        return "redirect:/users/findAll";
    }
    //删除用户
    @RequestMapping("/delete")
    public String delete(int id ){
        userService.delete(id);
        return "redirect:/users/findAll";
    }

    //跳转修改页面
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(HttpServletRequest request,int id){
        List<Role> all = roleService.findAll();
        Users users = userService.FindById(id);
        request.setAttribute("roleList",all);
        request.setAttribute("users",users);
        return "users/users-edit";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Users users,int roleId){
        userService.edit(users,roleId);
        return "redirect:/users/findAll";
    }

}
